// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.metrics;

import com.yahoo.document.restapi.OperationHandlerImpl;

import java.util.Set;

/**
 * Enum with possible outcomes of a single document feeding operation:
 * <ul>
 * <li>OK: Document was successfully added/updated/removed</li>
 * <li>REQUEST_ERROR: User-made error, for example invalid document format</li>
 * <li>SERVER_ERROR: Server-made error, for example insufficient disk space</li>
 * </ul>
 *
 * @author freva
 */
public enum DocumentOperationStatus {
    OK, REQUEST_ERROR, SERVER_ERROR;

    public static DocumentOperationStatus fromHttpStatusCode(int httpStatus) {
        switch (httpStatus / 100) {
            case 2: return OK;
            case 4: return REQUEST_ERROR;
            case 5: return SERVER_ERROR;
            default: return null;
        }
    }

    public static DocumentOperationStatus fromMessageBusErrorCodes(Set<Integer> errorCodes) {
        return fromHttpStatusCode(OperationHandlerImpl.getHTTPStatusCode(errorCodes));
    }
}
